package com.yuhong.controller;

import com.yuhong.pojo.Article;
import com.yuhong.pojo.PageBean;
import com.yuhong.pojo.Result;
import com.yuhong.service.ArticleService;
import com.yuhong.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);

        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum, Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);

        return Result.success(pb);
    }


    ///////////////////////////////////////////////////
    // 以下是一开始测试拦截器写的代码
    /*
    *****第一版本 不带拦截器
    * ***在controller里面判断token
    @GetMapping("/list")
    public Result<String> List(@RequestHeader(name = "Authorization") String token,
                               HttpServletResponse response) {
        // 验证token
        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            return Result.success("所有的文章数据");
        } catch (Exception e) {
            //throw new RuntimeException(e);
            response.setStatus(401);
            return Result.error("未登录");
        }
    }*/

    /*
    @GetMapping("/list")
    public Result<String> List() {
        return Result.success("所有的文章数据");
    }*/
}
